<template>
  <div class="Login">
    <div class="login-box">
      <div class="avatar-box">
        <el-image :src="logoImageUrl" fit="fit" class="logo"></el-image>
      </div>
      <!-- 表单 -->
      <el-form
        label-width="0px"
        :rules="loginRules"
        class="login-form"
        :model="form"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
            placeholder="请输入账户"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="remake">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 表单结尾 -->
    </div>
  </div>
</template>

<script>
import { login } from "@/network/login.js";
export default {
  name: "Login",
  data() {
    return {
      logoImageUrl: require("@/assets/logo.png"),
      // logoImageUrl: "@/assets/logo.png",
      //登陆表单数据
      form: {
        username: "admin",
        password: 123456,
      },
      //表单验证规则
      loginRules: {
        //验证用户名
        userName: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        //验证密码
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 登陆发送表单数据
    login() {
      let loginFormRef = this.$refs.loginFormRef;
      //进行表单预处理
      loginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        } else {
          const { data: res } = await login(this.form);
          // console.log(res);
          if (res.meta.status !== 200) {
            //登陆失败消息弹窗
            this.$message.error({
              message: "登陆失败，请重新登陆",
              type: "error",
              showClose: true,
            });
          } else {
            //登陆成功消息弹窗
            this.$message.success({
              message: "登陆成功",
              type: "success",
              showClose: true,
            });
            //保存token值
            // console.log(res.data.token)
            window.sessionStorage.setItem("token",res.data.token);
            this.$router.push({
              path: "/Home",
              query: {
                
              }
            });
          }
        }
      });
    },
    //重置表单内容
    remake() {
      let loginFormRef = this.$refs.loginFormRef;
      loginFormRef.resetFields(); //重置登陆表单
    },
  },
};
</script>

<style lang="less" scoped>
.Login {
  background-color: #2b4b6b;
  height: 100%;
  .login-box {
    width: 450px;
    height: 300px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: calc(50% - 225px);
    top: calc(50% - 150px);
  }
  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: calc(225px - 65px);
    top: calc(65px - 150px);
    background-color: #fff;
    .logo {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .login-form {
    width: 100%;
    padding: 0 10px;
    position: absolute;
    bottom: 0;
  }
}
</style>
